package com.account.book.controller;


import com.account.book.common.context.UserTokenContextHolder;
import com.account.book.common.response.BaseResult;
import com.account.book.common.response.ResultUtils;
import com.account.book.request.BookMoneyQueryReqVo;
import com.account.book.response.BookMoneyCountRes;
import com.account.book.service.BookMoneyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author liang-hw
 * @className BookMoneyController
 * @description 账本记账金额表 前端控制器
 * @date 2023/11/08 16:10:12
 */
@Api(tags = {"账本对应记账金接口"})
@RestController
@RequestMapping("/api/money")
public class BookMoneyController {
    @Autowired
    private BookMoneyService bookMoneyService;

    /**
     * @param @param bookMoneyQueryReqVo
     * @return @return {@link BaseResult }<{@link BookMoneyCountRes }>
     * @author liang-hw
     * @description 查询对应账本列表
     * @date 2023/11/06 17:05:11
     */
    @ApiOperation(value = "查询对应账本列表", notes = "查询对应账本列表")
    @PostMapping("/findList")
    public BaseResult<BookMoneyCountRes> findList(@Valid @RequestBody BookMoneyQueryReqVo bookMoneyQueryReqVo) {
        BookMoneyCountRes bookMoneyCountRes = bookMoneyService.findList(bookMoneyQueryReqVo);
        return ResultUtils.success(bookMoneyCountRes);
    }

    /**
     * @param @param bookMoneyQueryReqVo
     * @return @return {@link BaseResult }<{@link BookMoneyCountRes }>
     * @author liang-hw
     * @description 查询收支数量
     * @date 2023/11/06 17:29:00
     */
    @ApiOperation(value = "查询收支数量", notes = "查询收支数量")
    @PostMapping("/total")
    public BaseResult<BookMoneyCountRes> total(@Valid @RequestBody BookMoneyQueryReqVo bookMoneyQueryReqVo) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        BookMoneyCountRes bookMoneyCountRes = bookMoneyService.total(bookMoneyQueryReqVo, userId);
        return ResultUtils.success(bookMoneyCountRes);
    }

    /**
     * @param @param bookMoneyQueryReqVo
     * @return
     * @author liang-hw
     * @description 记一笔
     * @date 2023/11/08 17:13:48
     */
    @ApiOperation(value = "记账", notes = "记账")
    @PostMapping("/save")
    public BaseResult userAddBook(@Valid @RequestBody BookMoneyQueryReqVo bookMoneyQueryReqVo) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookMoneyService.addSave(bookMoneyQueryReqVo, userId);
        return ResultUtils.success();
    }

    /**
     * @param @param bookMoneyQueryReqVo
     * @return @return {@link BaseResult }
     * @author liang-hw
     * @description 修改一笔，算错数的时候，会修改金额
     * @date 2023/11/09 10:24:01
     */
    @ApiOperation(value = "修改一笔，算错数的时候，会修改金额", notes = "修改一笔，算错数的时候，会修改金额")
    @PostMapping("/update")
    public BaseResult update(@Valid @RequestBody BookMoneyQueryReqVo bookMoneyQueryReqVo) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookMoneyService.update(bookMoneyQueryReqVo, userId);
        return ResultUtils.success();
    }

    /**
     * @param @param moneyId
     * @return
     * @author liang-hw
     * @description 删除一笔
     * @date 2023/11/09 10:24:24
     */
    @ApiOperation(value = "删除一笔", notes = "删除一笔")
    @GetMapping("/delete")
    public BaseResult delete(@RequestParam("moneyId") String moneyId) {
        String userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        bookMoneyService.delete(moneyId, userId);
        return ResultUtils.success();
    }
}
